package no.ntnu.fp.gui;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import no.ntnu.fp.model.Appointment;
import no.ntnu.fp.model.Person;

import org.jdesktop.beansbinding.BeanProperty;
import java.sql.Date;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.Converter;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.GridLayout;

@SuppressWarnings("serial")
public class Popupmenu extends JDialog {

	private final JPanel contentPanel = new JPanel();
	final Appointment model;
	private JLabel datelbl;
	private JLabel titlelbl;
	private JLabel timelbl;
	private JTextArea textArea;
	private JButton cancelButton;
	private JButton changeButton;
	private JButton deleteButton;

	/**
	 * Create the dialog.
	 * 
	 * @param model
	 */
	public Popupmenu(final Appointment model) {
		this.model = model;

		setBounds(100, 100, 290, 272);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			titlelbl = new JLabel(model.getTitle());
			titlelbl.setBounds(10, 11, 119, 20);
			contentPanel.add(titlelbl);
		}

		textArea = new JTextArea();
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		textArea.setEditable(false);
		textArea.setText(model.getDescription());
		textArea.setBounds(10, 88, 187, 113);
		contentPanel.add(textArea);

		SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy");
		StringBuilder dvalue = new StringBuilder(dateformat.format(model
				.getStartDate()));
		datelbl = new JLabel(dvalue.toString());
		datelbl.setBounds(194, 11, 70, 20);
		contentPanel.add(datelbl);

		timelbl = new JLabel(model.getStart().toString() + " - "
				+ model.getEnd().toString());
		timelbl.setBounds(10, 42, 132, 14);
		contentPanel.add(timelbl);

		JPanel buttonPane = new JPanel();
		getContentPane().add(buttonPane, BorderLayout.SOUTH);

		cancelButton = new JButton("Avbryt");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				processWindowEvent(new WindowEvent(Popupmenu.this,
						WindowEvent.WINDOW_CLOSING));
			}
		});
		buttonPane.setLayout(new GridLayout(0, 3, 0, 0));
		buttonPane.add(cancelButton);

		changeButton = new JButton("Endre");
		if (model.owner != Person.getCurrentUser().id) {
			changeButton.setEnabled(false);
		}
		changeButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent ev) {
				if (model.owner == Person.getCurrentUser().id) {
					new NewAppointmentWindow(model);
				}
			}
		});
		// changeButton.setActionCommand("OK");
		buttonPane.add(changeButton);
		getRootPane().setDefaultButton(changeButton);

		deleteButton = new JButton("Slett");
		if (model.owner != Person.getCurrentUser().id) {
			deleteButton.setEnabled(false);
		}
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (model.owner == Person.getCurrentUser().id
						&& JOptionPane.NO_OPTION == JOptionPane
								.showConfirmDialog(
										Popupmenu.this,
										"Er du sikker på at du vil slette denne avtalen?",
										"Slett avtale",
										JOptionPane.YES_NO_OPTION,
										JOptionPane.WARNING_MESSAGE)) {
					model.delete();
				}

			}
		});

		// deleteButton.setActionCommand("Slett");
		buttonPane.add(deleteButton);
	}

	public final class DateConverter extends Converter<Date, Object> {

		public DateConverter() {

		}

		public Date convertReverse(Object arg) {
			// JLabel is "read-only"

			// Add your reverse code when it is a combo box, text field etc.
			// that returns Date object, for example:

			java.util.Date date = new java.util.Date(); // Default for error
			// handling
			try {
				DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
				date = formatter.parse((String) arg); // String to Date object
			} catch (Exception ex) {
				System.out.println("dateConverter.convertReverse error: "
						+ ex.getMessage());
			}
			return (Date) date; // return Date object
		}

		public String convertForward(Date value) {
			SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy");
			StringBuilder svalue = new StringBuilder(dateformat.format(value));

			return svalue.toString();
		}

	}
}
